//
//  XLEmojiInputView.m
//
//
//  Created by ilongge on 2020/8/4.
//

#import "XLEmojiManager.h"
#import "XLEmojiInputView.h"
#import "XLEmojiInputViewCell.h"
#import "XLConversationInputViewDef.h"
@interface XLEmojiInputView()
<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) UICollectionView *emojiCollection;
@property (nonatomic, strong) NSMutableArray *emojiNameArray;
@property (nonatomic, strong) NSMutableArray *emojiTitleArray;
@property (nonatomic, assign) CGSize emojiSize;
@property (nonatomic, strong) UIButton *deleteButton;
@end
@implementation XLEmojiInputView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self parperData];
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    NSInteger col = self.frame.size.width/40;
    NSInteger row = (self.frame.size.height-kXLCIBottomSafeHeight)/40;
    CGFloat width = self.frame.size.width/col;
    CGFloat height = (self.frame.size.height-kXLCIBottomSafeHeight)/row;
    if (width<=0) {
        width = 40;
    }
    if (height<=0) {
        height = 40;
    }
    self.emojiSize = CGSizeMake(width, height);
    [self addSubview:self.emojiCollection];
    [self addSubview:self.deleteButton];
    [self.emojiCollection registerClass:[XLEmojiInputViewCell class]
             forCellWithReuseIdentifier:[[XLEmojiInputViewCell class] description]];
    [self.emojiCollection reloadData];
}

- (void)parperData
{
    NSArray *allEmojiArray = [XLEmojiManager emojiImageNameArray];
    for (NSDictionary *item in allEmojiArray) {
        [self.emojiNameArray addObject:item[@"name"]];
        [self.emojiTitleArray addObject:item[@"text"]];
    }
    NSInteger count = self.frame.size.width/40;
    if (count && self.emojiNameArray.count % count == 0) {
        [self.emojiNameArray addObject:@""];
        [self.emojiTitleArray addObject:@""];
    }
}

- (void)deleteEmoji
{
    if (self.deleteEmojiBlock) {
        self.deleteEmojiBlock();
    }
}

- (void)showEmojiInputView
{
    UIWindow *currentWindow = nil;
    currentWindow = [UIApplication sharedApplication].keyWindow;
    if (currentWindow) {
        [currentWindow addSubview:self];
        [UIView animateWithDuration:0.3
                         animations:^{
            self.transform = CGAffineTransformMakeTranslation(0, - self.frame.size.height);
        }];
    }
}

- (void)hiddenEmojiInputView
{
    [UIView animateWithDuration:0.3
                     animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)layoutSubviews
{
    self.emojiCollection.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-kXLCIBottomSafeHeight);
    self.deleteButton.frame = CGRectMake(self.frame.size.width-self.emojiSize.width,
                                         CGRectGetMaxY(self.emojiCollection.frame)-self.emojiSize.height,
                                         self.emojiSize.width,
                                         self.emojiSize.height);
}

#pragma mark UICollectionViewDelegate / UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = self.emojiNameArray.count;
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XLEmojiInputViewCell *cell = [XLEmojiInputViewCell emojiInputViewCellWith:collectionView at:indexPath];
    NSString *emojiName = self.emojiNameArray[indexPath.row];
    [cell configCellWithEmojiName:emojiName];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.inputEmojiBlock) {
        NSString *emojiTitle = self.emojiTitleArray[indexPath.row];
        NSString *emojiName = self.emojiNameArray[indexPath.row];
        UIImage *image = [XLEmojiManager JFLiveEmojiImageWithName:emojiName];
        self.inputEmojiBlock(emojiTitle, emojiName, image);
    }
//    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
 
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor lightGrayColor];
}
 
- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
}
#pragma mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return self.emojiSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.1;
}

#pragma mark 懒加载
- (UICollectionView *)emojiCollection
{
    if (_emojiCollection == nil) {
        UICollectionViewFlowLayout *collectionLayOut = [[UICollectionViewFlowLayout alloc] init];
        _emojiCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,0,0)
                                              collectionViewLayout:collectionLayOut];
        _emojiCollection.delegate = self;
        _emojiCollection.dataSource = self;
        _emojiCollection.backgroundColor = [UIColor whiteColor];
    }
    return _emojiCollection;
}

- (UIButton *)deleteButton
{
    if (_deleteButton == nil) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.backgroundColor = [UIColor whiteColor];
        _deleteButton.layer.cornerRadius = 5.0;
        _deleteButton.clipsToBounds = YES;
        [_deleteButton setImage:[XLEmojiManager JFLiveEmojiImageWithName:@"emoji_del"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteEmoji) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (NSMutableArray *)emojiNameArray
{
    if (_emojiNameArray == nil) {
        _emojiNameArray = [[NSMutableArray alloc] init];
    }
    return _emojiNameArray;
}

- (NSMutableArray *)emojiTitleArray
{
    if (_emojiTitleArray == nil) {
        _emojiTitleArray = [[NSMutableArray alloc] init];
    }
    return _emojiTitleArray;
}

@end
